/**
 * Created by Administrator on 2015/1/22.
 */
var global_path = location.host !='www.12301.cc' ? '/new/d' : '';
var WXB = {
	FN : new Function,
	URLS : {
		api : 'http://'+location.host+global_path+'/module/wechat/ichat.php'
	},
	AJAX_TIMEOUT : 10 * 1000,
	G : {
		//切换公众号模块
		SwitchAccount : (function(){
			var onChangeCbs = [];
			var loading = false;
			var oData = null;
			var curHash = "";
			var curActype = "";
			var curName = "";
			var getAccountList = function(config){
				var config = config || {};
				var loading = config.loading || WXB.FN;
				var removeLoading = config.removeLoading || WXB.FN;
				var success = config.success || WXB.FN;
				var empty = config.empty || WXB.FN;
				var fail = config.fail || WXB.FN;
				var timeout = config.timeout || WXB.FN;
				var error = config.error || WXB.FN;
				$.ajax({
					url : WXB.URLS.api,
					type : "GET",
					dataType : "JSON",
					timeout : WXB.AJAX_TIMEOUT,
					data : { action:"GetWechatBinded"},
					beforeSend : function(){
						loading();
					},
					success : function(res){
						removeLoading();
						if(res && res.status == "ok"){
							if(res.list && res.list.length){
								success(res);
							}else{
								empty(res);
							}
						}else if(res && res.status == "fail"){
							fail(res.msg);
						}else{
							error();
						}
					},
					error : function(xhr,txt){
						removeLoading();
						if(txt == "timeout"){
							timeout();
						}else{
							error();
						}
					}
				})
			};
			var refreshAccountDetail = function(data){
				data = data || {};
				var def = '<span style="font-style:italic; color:#adadad">（空）</span>';
				var hash = data.hash || def;
				var type = data.type || def;
				var token = data.token || def;
				var url = data.url || def;
				var aeskey = data.aeskey || def;
				var name = data.name || def;
				$("#account_type").html(type);
				$("#account_url").html(url);
				$("#account_token").html(token);
				$("#account_EncodingAESKey").html(aeskey);
				$("#account_name").html(name);
			}
			function init(callback){
				$("#account_list").on("change",function(e){
					var targetOpt = $("#account_list").find("option:selected");
					var data = {};
					data["hash"] = targetOpt.attr("data-hash");
					data["type"] = targetOpt.attr("data-actype");
					data["url"] = targetOpt.attr("data-url");
					data["token"] = targetOpt.attr("data-token");
					data["aeskey"] = targetOpt.attr("data-encodingaeskey");
					data["name"] = targetOpt.attr("data-name");
					curHash = data["hash"];
					curActype = data["type"];
					curName = data["name"];
					refreshAccountDetail({
						hash : data.hash,
						type : data.type,
						url : data.url,
						token : data.token,
						aeskey : data.aeskey,
						name : data.name
					});
					for(var i in onChangeCbs){
						onChangeCbs[i](data);
					}
				})
				getAccountList({
					loading : function(){ loading = true},
					removeLoading : function(){ loading = false},
					success : function(res){
						oData = res.list;
						var data = res.list;
						var html = "";
						for(var i=0; i<data.length; i++){
							var d = data[i];
							var ac_type = d.ac_type;
							var create_time = d.create_time;
							var encodingAESKey = d.encodingAESKey;
							var hash = d.id;
							var name = d.name;
							var token = d.token;
							var username = d.username;
							var url = d.url;
							if(i==0){
								curHash = hash;
								curActype = ac_type;
								curName = name;
							}
							html += '<option value="'+hash+'" data-hash="'+hash+'" data-actype="'+ac_type+'" data-createtime="'+create_time+'" data-encodingAESKey="'+encodingAESKey+'" data-name="'+name+'" data-username="'+username+'" data-token="'+token+'" data-url="'+url+'">'+name+'</option>';
						}
						$("#account_list").html(html);
						var data = WXB.G.SwitchAccount.getData(0);
						refreshAccountDetail(data);
						callback && callback();
					},
					fail : function(failMsg){
						alert("GetWechatBinded接口返回失败:"+failMsg);
					}
				})
			}
			function on(type,callback){ // type - change / onInitAfter
				if(type == "change" && typeof callback == "function"){
					onChangeCbs.push(callback);
				}
			}
			function getStatus(){
				return loading;
			}
			function getData(index){
				index = index || 0;
				if(!oData) return false;
				var d = oData[index];
				var data = {
					hash : d.hash,
					type : d.ac_type,
					url : d.url,
					token : d.token,
					aeskey : d.encodingAESKey,
					name : d.name
				};
				return data;
			}
			function getHash(){
				return curHash;
			}
			function getActype(){
				return curActype;
			}
			function getName(){
				return curName;
			}
			return{
				getHash : getHash,
				getActype : getActype,
				getStatus : getStatus, //弃用
				getAccountList : getAccountList,
				getData : getData,
				getName : getName,
				on : on,
				init : init
			}
		})(),
		Store : (function(){
			function set(key,value){
				var store = window.localStorage;
				if(store){
					store.setItem(key,value);
				}else{
					$.cookie(key,value);
				}
			}
			function get(key){
				var store = window.localStorage;
				return store ? store.getItem(key) : $.cookie(key);
			}
			return{ get:get, set:set}
		})(),
		Delay : (function(){
			var timer = null;
			function set(fn,time){
				var time = time || 3 * 1000;
				if(timer){
					clearTimeout(timer);
				}
				timer = setTimeout(function(){
					fn();
				},time)
			}
			function clear(){
				if(timer) clearTimeout(timer);
			}
			return{
				set : set,
				clear : clear
			}
		})(),
		Alert : (function(){
			/**
			 * @param content  显示的内容
			 * @param duration 显示多长时间后关闭
			 */
			return function(type,content,duration,callback){
				var type  = type || "success";
				var duration = duration || 2 * 1000;
				if(!content) return false;
				var wrap = $("#gTopAlertContainer");
				if(!wrap.length){
					wrap = $('<div id="gTopAlertContainer"></div>');
					$("body").append(wrap);
				}
				wrap.removeClass("success").removeClass("error").removeClass("fail");
				wrap.addClass(type);
				wrap.html(content);
				setTimeout(function(){
					wrap = $("#gTopAlertContainer");
					var width = wrap.outerWidth(true);
					var height = wrap.outerHeight(true);
					wrap.css({"marginLeft":-width/2,"top":-height-2});
					wrap.animate({top:0},"normal",function(){
						WXB.G.Delay.set(function(){
							wrap.animate({top:-height-2},"normal",function(){
								callback && callback();
							})
						},duration)
					})
				},0)
			}
		})()
	}
};
